﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Reflection;
using System.IO;
using Microsoft.Xna.Framework.Input;

namespace TerenXNA
{
    public class ScreenMenu
    {
        private int menuWidth;
        private int menuHeight;
        public Rectangle menuRect;
        private SpriteBatch spriteBatch;
        private Texture2D texture;
        private Texture2D solidTexture;
        public static SpriteFont spriteFont;
        private World world;

        private ScreenMenuButton[] screenMenuButtons;
        private List<Slider> sliders;

        public static int buttonWidth, buttonHeight;

        //czy okno jest wyswietlane aktualnie na ekranie
        public bool Showed = false;
        //czy przycisk wlaczenia okna jest nacisniety
        public bool DisplayGUIButtonPressed = false;

        public enum CameraType { Debug=2, Fly=0, Ground=1, Static=3 }
        public static CameraType actualCameraMode = CameraType.Static;



        public ScreenMenu(World world)
        {
            this.world = world;
            menuHeight = Game1.device.DisplayMode.Height;
            menuWidth = Game1.device.DisplayMode.Width / 9;
            menuRect = new Rectangle(0, 0, menuWidth, menuHeight);
            spriteBatch = new SpriteBatch(Game1.device);
            spriteFont = Game1.contentManager.Load<SpriteFont>("Arial2");
            texture = new Texture2D(Game1.device, menuWidth, menuHeight);
            texture.SetData(PrepareTextureBackgroundColor(menuWidth, menuHeight, 192, 192, 192, 100));

            solidTexture = new Texture2D(Game1.device, 1, 1);
            solidTexture.SetData(PrepareTextureBackgroundColor(1, 1, 255, 140, 0, 255));

            buttonWidth = menuWidth - 20;
            buttonHeight = menuHeight / 20;

            screenMenuButtons = new ScreenMenuButton[2];
            screenMenuButtons[0] = new ScreenMenuButton(10, 10, "Kamera", this.spriteBatch, this.solidTexture);
            screenMenuButtons[0].clickActionDelegate = SwitchToNextCameraMode;
            /*screenMenuButtons[1] = new ScreenMenuButton(10, 150, "Lat/Lon info", this.spriteBatch, this.solidTexture);
            screenMenuButtons[1].clickActionDelegate = SwicthDisplayTextViewer;
            screenMenuButtons[2] = new ScreenMenuButton(10, 210, "Accuracy", this.spriteBatch, this.solidTexture);
            screenMenuButtons[2].clickActionDelegate = SwitchTextViewerAccuracy;
            screenMenuButtons[3] = new ScreenMenuButton(10, 270, "Texture", this.spriteBatch, this.solidTexture);
            screenMenuButtons[3].clickActionDelegate = SwitchTextureDisplay;
            screenMenuButtons[4] = new ScreenMenuButton(10, 330, "Height map", this.spriteBatch, this.solidTexture);
            screenMenuButtons[4].clickActionDelegate = SwitchHeightMapDisplay;*/
            screenMenuButtons[1] = new ScreenMenuButton(10, 150, "Siatka terenu", this.spriteBatch, this.solidTexture);
            screenMenuButtons[1].clickActionDelegate = SwitchGridDisplay;

            sliders = new List<Slider>();
            /*
            sliders.Add(new Slider(180, 10, 270, this.solidTexture));
            sliders.Add(new Slider(180, 10, 350, this.solidTexture));
            sliders.Add(new Slider(180, 10, 430, this.solidTexture));
            */
            // światło
            sliders.Add(new Slider(140, 20, 350, this.solidTexture));
            sliders[0].ForceValue(-2);

            // zakres widoczności
            sliders.Add(new Slider(140, 20, 510, this.solidTexture));
            sliders[1].ForceValue(-5);

            // wysokość terenu
            sliders.Add(new Slider(140, 20, 670, this.solidTexture));
            sliders[2].ForceValue(-5);
        }

        public void Update()
        {
            MouseState mouseState = Mouse.GetState();

            foreach (ScreenMenuButton btn in screenMenuButtons)
            {
                btn.MouseUpdate(mouseState);
            }
            for (int i = 0; i < sliders.Count; i++)
            {
                sliders[i].MouseUpdate(mouseState);
                world.Angles[i] = sliders[i].GetValue();
            }

            //world.aktCam.coords.farValue = ((sliders[1].GetValue() + 5) * 3000) + 30000;
            world.aktCam.coords.farValue = ((sliders[1].GetValue() + 5) * 6000) + 20000;
            world.uLod = (int) (((sliders[1].GetValue() + 5) * 5) + 20);

            Game1.heightScale = (sliders[2].GetValue() + 5) + 1;
            if (Game1.heightScale < 1.2f)
            {
                Game1.heightScale = 1.0f;
            }
        }



        public static void SwitchTextViewerAccuracy()
        {
            TextViewer.accuracy += 1;
            TextViewer.accuracy %= 3;
        }

        public static void SwicthDisplayTextViewer()
        {
            TextViewer.displayGUI = !TextViewer.displayGUI;
        }

        public static void SwitchTextureDisplay()
        {
            World.isTextureVisible = !World.isTextureVisible;   
        }

        public static void SwitchHeightMapDisplay()
        {
            World.isHeightMapVisible = !World.isHeightMapVisible;
        }

        public static void SwitchToNextCameraMode()
        {
            switch (ScreenMenu.actualCameraMode)
            {
                case CameraType.Debug:
                    ScreenMenu.actualCameraMode =  CameraType.Fly;
                    break;
                case CameraType.Fly:
                    ScreenMenu.actualCameraMode = CameraType.Ground;
                    break;
                case CameraType.Ground:
                    ScreenMenu.actualCameraMode = CameraType.Static;
                    break;
                case CameraType.Static:
                    ScreenMenu.actualCameraMode = CameraType.Debug;
                    break;
            }
        }

        public void SwitchGridDisplay()
        {
            World.isGridVisible = !World.isGridVisible;
        }

        /*public static CameraType SwitchToNextCameraMode()
        {
            switch (ScreenMenu.actualCameraMode)
            {
                case CameraType.Debug:
                    return CameraType.Fly;
                case CameraType.Fly:
                    return CameraType.Ground;
                case CameraType.Ground:
                    return CameraType.Static;
                case CameraType.Static:
                    return CameraType.Debug;
            }

            return CameraType.Static;
        }*/

        private static String GetCameraNameString(CameraType cameratype)
        {
            switch (cameratype)
            {
                case CameraType.Debug:
                    return "Tryb testowania";
                case CameraType.Fly:
                    return "Tryb powietrzny";
                case CameraType.Ground:
                    return "Tryb naziemny";
                case CameraType.Static:
                    return "Tryb nieruchomy";
            }

            return "Nieznany";

        }

        public static Color[] PrepareTextureBackgroundColor(int width, int height, int r, int g, int b, int alpha)
        {
            Color[] backgroundColor = new Color[width * height];
            for (int i = 0; i < width * height; i++)
            {
                backgroundColor[i] = new Color(r, g, b, alpha);
            }

            return backgroundColor;
        }

        public void Draw()
        {
            if (this.Showed == true)
            {
                spriteBatch.Begin();
                spriteBatch.Draw(texture, menuRect, Color.Silver);
                foreach (ScreenMenuButton btn in screenMenuButtons)
                {
                   btn.Draw();
                }
                foreach (Slider slider in sliders)
                {
                    slider.Draw(spriteBatch);
                }
                spriteBatch.DrawString(spriteFont, GetCameraNameString(actualCameraMode), new Vector2(10, 70), Color.Black);
                spriteBatch.DrawString(spriteFont, "Pozycja swiatla", new Vector2(10, 280), Color.Black);
                spriteBatch.DrawString(spriteFont, "Poziom detali", new Vector2(10, 430), Color.Black);
                //spriteBatch.DrawString(spriteFont, "Height Map: " + World.isHeightMapVisible.ToString(), new Vector2(10, 530), Color.Black);
                spriteBatch.DrawString(spriteFont, "Wysokosc terenu", new Vector2(10, 590), Color.Black);
                
                spriteBatch.End();
            }
        }

    }
}
